package com.ms.oschina.ui.activity;

import android.annotation.SuppressLint;
import android.support.v4.app.Fragment;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.webkit.JavascriptInterface;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.FrameLayout;

import com.ms.oschina.R;
import com.ms.oschina.base.BaseDetailsActivity;
import com.ms.oschina.fragment.NewsDetailBottomEmojiFragment;
import com.ms.oschina.interf.OnWebViewImageListener;
import com.ms.oschina.util.StringUtils;
import com.ms.oschina.util.UiUtils;

/**
 * 包括 新闻详情页 ，博客详情页，活动详情页 的基类
 * 
 * @author Administrator
 * 
 */
public abstract class WebpageDetailsActivity extends BaseDetailsActivity {
	private WebView wvDetailsContent;
	private NewsDetailBottomEmojiFragment newsDetailBottomFragment;
	private View rootView;

	protected View loadSuccessView() {
		rootView = UiUtils.inflate(R.layout.activity_newdetails);
		findViews(rootView);
		setData();
		return rootView;
	}

	private void setData() {
		FrameLayout title = (FrameLayout) rootView
				.findViewById(R.id.fl_details_content);
		View titleFragment = getTitleView();
		if (titleFragment != null) {
			title.addView(titleFragment);
		}

		initWebViewSeting();
		wvDetailsContent.setWebViewClient(new WebViewClient() {
			// 网页加载结束
			@Override
			public void onPageFinished(WebView view, String url) {
				super.onPageFinished(view, url);
			}

			// 所有链接跳转会走此方法
			@Override
			public boolean shouldOverrideUrlLoading(WebView view, String url) {
				view.loadUrl(url);// 在跳转链接时强制在当前webview中加载
				return true;
			}
		});

		onSuccessViewData();
	}

	public abstract void onSuccessViewData();

	@SuppressLint({ "JavascriptInterface" })
	private void initWebViewSeting() {
		// 允许执行javascript语句
		WebSettings settings = wvDetailsContent.getSettings();
		// 缩放开关 设置此属性，仅支持双击缩放，不支持触摸缩放（在android4.0是这样，其他平台没试过）
		settings.setSupportZoom(false);
		settings.setJavaScriptEnabled(true);
		settings.setDefaultFontSize(15);
		// 设置webview的宽度自适应屏幕的宽度
		settings.setLayoutAlgorithm(WebSettings.LayoutAlgorithm.SINGLE_COLUMN);
		settings.setLoadWithOverviewMode(true);
		wvDetailsContent.setHorizontalScrollBarEnabled(false);// 水平滚动条不显示
		wvDetailsContent.setVerticalScrollBarEnabled(false); // 垂直滚动条不显示
		settings.setUserAgentString("MicroMessager");
		wvDetailsContent.addJavascriptInterface(new OnWebViewImageListener() {
			@Override
			@JavascriptInterface
			public void showImagePreview(String bigImageUrl) {
				if (!StringUtils.isEmpty(bigImageUrl)) {
					UiUtils.showImagePreview(WebpageDetailsActivity.this,null, bigImageUrl);
				}
			}
		}, "mWebViewImageListener");
	}

	private void findViews(View view) {
		wvDetailsContent = (WebView) view.findViewById(R.id.wv_details_content);
	}

	public abstract View getTitleView();

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		getMenuInflater().inflate(R.menu.refresh_menu, menu);
		return super.onCreateOptionsMenu(menu);
	}

	@Override
	public boolean onMenuItemSelected(int featureId, MenuItem item) {
		switch (item.getItemId()) {
		case R.id.public_menu_shared: {
			UiUtils.showToast("刷新数据");
			initLoad();
			return true;
		}
		}
		return super.onMenuItemSelected(featureId, item);
	}
	
	
	/**
	 * 重新加载数据
	 */
	protected void initLoad() {
		mLp.init();
	}

	@Override
	protected void onDestroy() {
		ViewGroup parent = (ViewGroup) wvDetailsContent.getParent();
		parent.removeView(wvDetailsContent);
		wvDetailsContent.destroy();
		super.onDestroy();
	}

	public WebView getWvDetailsContent() {
		return wvDetailsContent;
	}

	public NewsDetailBottomEmojiFragment getBottomDetailControlFragment() {
		return newsDetailBottomFragment;
	}

	@Override
	protected Fragment bottomControlFragement() {
		newsDetailBottomFragment = (NewsDetailBottomEmojiFragment) Fragment
				.instantiate(this,
						NewsDetailBottomEmojiFragment.class.getName());
		return newsDetailBottomFragment;
	}
}
